package com.byq.mm.application.cluster.server;

import java.util.List;


import com.byq.mm.abstractx.device.Device;
import com.byq.mm.abstractx.engine.CoreEnvironment;
import com.byq.mm.abstractx.protocol.Interaction;
import com.byq.mm.application.cluster.provider.LevelControlProvider;
import com.byq.mm.application.cluster.provider.PlayerProvider;
import com.byq.mm.application.cluster.server.executor.LevelControlExecutor;


import com.byq.mm.application.cluster.server.executor.PlayerExecutor;
import com.byq.mm.foundation.cluster.datatype.AttributeValue;
import com.byq.mm.foundation.cluster.datatype.BooleanValue;
import com.byq.mm.foundation.cluster.datatype.DoubleRangeValue;
import com.byq.mm.foundation.cluster.datatype.DoubleValue;
import com.byq.mm.foundation.cluster.datatype.IntValue;
import com.byq.mm.foundation.cluster.datatype.StringListValue;
import com.byq.mm.foundation.cluster.server.BasicClusterServer;

public class PlayerServer extends BasicClusterServer {

	public PlayerServer(String cluster, Device device, CoreEnvironment env) {
		super(cluster, device, env);
	}

	@Override
	protected Class<? extends AttributeValue> getAttributeType(String attributeName) {
		
		return PlayerProvider.Define.attributeTypes.get(attributeName);
	}

	@Override
	protected void init() {
		attributes = PlayerProvider.Define.init();
		
	}
	@Override
	public boolean onCommand(Interaction i, String from) {
		if(super.onCommand(i, from))
			return true;
		
		
		PlayerExecutor e = (PlayerExecutor)this.getExecutor();
		if(e == null)
			return false;
		
			
			if(i.command.equals(PlayerProvider.Define.ClientCommand.MUTE)){
				e.toggleMute();
				return true;
			}

		return false;
		
		
		
	}
	@Override
	protected void attributeWritten(String attributeName, AttributeValue value) {
		PlayerExecutor e = (PlayerExecutor)this.getExecutor();
		if(e == null)
			return;
		if(attributeName.equals(PlayerProvider.Define.Attributes.STATUS)){
			int s = ((IntValue)value).getValue();
			if(s == 1)
				e.play();
			if(s == 0)
				e.stop();
			if(s == 3)
				e.pause();
		}
		if(attributeName.equals(PlayerProvider.Define.Attributes.VOLUME)){
			int s = ((IntValue)value).getValue();
			e.volume(s);
		}
		if(attributeName.equals(PlayerProvider.Define.Attributes.PROGRESS)){
			int s = ((IntValue)value).getValue();
			e.progress(s);
		}
		if(attributeName.equals(PlayerProvider.Define.Attributes.CURRENT_INDEX)){
			int s = ((IntValue)value).getValue();
			e.select(s);
		}
		if(attributeName.equals(PlayerProvider.Define.Attributes.ACTIVE)){
			boolean s = ((BooleanValue)value).isValue();
			if(s)
				e.startSession();
			else{
				e.stopSession();
				reset();
			}
		}
	}
	void reset(){
		init();
		for(String k : this.attributes.keySet())
			this.broadcastAttributeToAll(k);
	}
	public void onSelect(int index){
		IntValue sv = new IntValue(index);
		attributes.put(PlayerProvider.Define.Attributes.CURRENT_INDEX, sv);
		this.broadcastAttributeToAll(PlayerProvider.Define.Attributes.CURRENT_INDEX);
	}
	public void onProgress(int p){
		IntValue sv = new IntValue(p);
		attributes.put(PlayerProvider.Define.Attributes.PROGRESS, sv);
		this.broadcastAttributeToAll(PlayerProvider.Define.Attributes.PROGRESS);
	}
	public void onCurrentLength(int length){
		IntValue sv = new IntValue(length);
		attributes.put(PlayerProvider.Define.Attributes.CURRENT_LENGTH, sv);
		this.broadcastAttributeToAll(PlayerProvider.Define.Attributes.CURRENT_LENGTH);
	}
	public void onPlay(){
		onStatus(1);
		
	}
	public void onStatus(int s){
		IntValue sv = new IntValue(s);
		attributes.put(PlayerProvider.Define.Attributes.STATUS, sv);
		this.broadcastAttributeToAll(PlayerProvider.Define.Attributes.STATUS);
	}
	public void onVolume(int v){
		IntValue sv = new IntValue(v);
		attributes.put(PlayerProvider.Define.Attributes.VOLUME, sv);
		this.broadcastAttributeToAll(PlayerProvider.Define.Attributes.VOLUME);
	}

	public void onPause(){
		onStatus(3);
	}
	public void onStop(){
		onStatus(0);
	}
	public void playList(List<String> list){
		StringListValue sv = new StringListValue(list);
		attributes.put(PlayerProvider.Define.Attributes.PLAYLIST, sv);
		this.broadcastAttributeToAll(PlayerProvider.Define.Attributes.PLAYLIST);
		
	}

}
